package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.constant.MessageConstant;
import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.mqtt.entity.MsgBodySensorBind;
import com.csun.cmny.mqtt.entity.MsgHumidistatBind;
import com.csun.cmny.mqtt.entity.MsgHumidistatTimer;
import com.csun.cmny.mqtt.entity.MsgLifeBodySensorTimer;
import com.csun.cmny.provider.controller.websocket.WebSocketUtils;
import com.csun.cmny.provider.device.card.CardMessageHandler;
import com.csun.cmny.provider.device.csun.camera.CameraMessageHandler;
import com.csun.cmny.provider.device.csun.gateway.CsunGatewayMessageGateway;
import com.csun.cmny.provider.device.csun.gateway.GatewayMessageSender;
import com.csun.cmny.provider.device.csun.sleep.SleepMessageGateway;
import com.csun.cmny.provider.device.csun.sleep.SleepMessageSender;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.DmcCsunVersionFile;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcRfidEvent;
import com.csun.cmny.provider.model.dto.DeviceAlertDto;
import com.csun.cmny.provider.model.dto.SendMessageRequest;
import com.csun.cmny.provider.model.dto.card.CardOptDoorDto;
import com.csun.cmny.provider.model.dto.device.DeviceStatus;
import com.csun.cmny.provider.model.dto.patrol.PatrolRecordQueryDto;
import com.csun.cmny.provider.model.dto.patrol.PatrolReminderQueryDto;
import com.csun.cmny.provider.model.dto.propertyManagement.DeviceDeriveExcelDto;
import com.csun.cmny.provider.model.dto.rfid.RfidEventPageQuery;
import com.csun.cmny.provider.model.dto.version.DeviceUpdateDto;
import com.csun.cmny.provider.model.dto.version.SetVersionDto;
import com.csun.cmny.provider.model.vo.patrol.PatrolRecordVo;
import com.csun.cmny.provider.model.vo.patrol.PatrolReminderVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyManagementDeviceVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.ExcelUtil;
import com.csun.cmny.provider.utils.JxlExcelUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-12 14:39
 * @Description:
 */
@RestController
@RequestMapping(value = "/api")
public class DmcApiController  {

    @Autowired
    private OpcMessageService opcMessageService;
    @Resource
    private DmcCsunVersionFileService dmcCsunVersionFileService;
    @Resource
    private CsunGatewayMessageGateway gatewayMessageGateway;
    @Resource
    private SleepMessageGateway sleepMessageGateway;
    @Resource
    private OmcRpcService omcRpcService;
    @Resource
    private CardMessageHandler cardMessageHandler;
    @Resource
    private GatewayMessageSender gatewayMessageSender;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private WebSocketUtils webSocketUtils;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private CameraMessageHandler cameraMessageHandler;
    @Resource
    private SleepMessageSender sleepMessageSender;
    @Resource
    private CmnyProperties cmnyProperties;
    @Resource
    private DmcPropertyManagementService propertyManagementService;
    @Resource
    private DmcRfidEventService dmcRfidEventService;
    @Resource
    private DmcPatrolRecordService dmcPatrolRecordService;
    @Resource
    private DmcPatrolReminderService dmcPatrolReminderService;

    @PostMapping(value = "/sendMessageWithUser")
    public Wrapper sendMessageWithUser(@RequestBody SendMessageRequest sendMessageRequest) {

        opcMessageService.pushByUser(sendMessageRequest);

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @PostMapping(value = "/sendMessageWithCode")
    public Wrapper sendMessageWithCode(@RequestBody SendMessageRequest sendMessageRequest) {

        opcMessageService.pushWithMsgCode(sendMessageRequest);

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @PostMapping(value = "/sendMessageWithGroupId")
    public Wrapper sendMessageWithGroupId(Long groupId, String message) {

        SendMessageRequest sendMessageRequest = new SendMessageRequest();
        sendMessageRequest.setGroupId(groupId);
        sendMessageRequest.setMessage(message);
        sendMessageRequest.setMsgCode(MessageConstant.MESSAGE_CODE_WEB_ALERT_REFRESH);

        opcMessageService.pushWithMsgCode(sendMessageRequest);

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/sendToMqtt", method = RequestMethod.POST)
    public Wrapper sendToMqtt(String topic, String message) {

        sleepMessageGateway.sendToMqtt(topic, message);

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/gatewaySendToMqtt", method = RequestMethod.POST)
    public Wrapper gatewaySendToMqtt(String topic, String message) {

        gatewayMessageGateway.sendToMqtt(topic, message);

        // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/sendToMqttWithQos", method = RequestMethod.POST)
    public Wrapper sendToMqttWithQos(String topic, Integer qos, String message) {

        sleepMessageGateway.sendToMqtt(topic, qos, message);

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/sendToMqttWithList", method = RequestMethod.POST)
    public Wrapper sendToMqttWithList(String topic, Integer qos, String message) {

        List<String> topicList = Arrays.asList(topic.split(","));
        for (String t : topicList) {
            sleepMessageGateway.sendToMqtt(t, qos, message);
        }

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/gatewayBindHumidistatList", method = RequestMethod.POST)
    public Wrapper gatewayBindHumidistatList(@RequestBody MsgHumidistatBind msgHumidistatBind) {

        //gatewayMessageSender.bindHumidistat(msgHumidistatBind.getNetgateId(), msgHumidistatBind.getHumidistatList());

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/gatewayBindBodySensor", method = RequestMethod.POST)
    public Wrapper gatewayBindBodySensor(@RequestBody MsgBodySensorBind msgBodySensorBind) {

        //gatewayMessageSender.bindBodySensor(msgBodySensorBind.getNetgateId(), msgBodySensorBind.getBodySensorList());

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/setHumidistatTimer", method = RequestMethod.POST)
    public Wrapper setHumidistatTimer(@RequestBody MsgHumidistatTimer msgHumidistatTimer) {

        gatewayMessageSender.humidistatTimer(msgHumidistatTimer.getNetgateId(),
                msgHumidistatTimer.getHumidistatId(), msgHumidistatTimer.getDuration());

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/setLifeBodySensorTimer", method = RequestMethod.POST)
    public Wrapper setLifeBodySensorTimer(@RequestBody MsgLifeBodySensorTimer msgLifeBodySensorTimer) {

        gatewayMessageSender.setBodySensorTimer(msgLifeBodySensorTimer);
       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @ValidateAnnotation
    @PostMapping(value = "/setVersion")
    public Wrapper setVersion(@Validated @RequestBody SetVersionDto setVersionDto,
                              BindingResult br) {

        Integer result = dmcCsunVersionFileService.setVersion(setVersionDto);

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @PostMapping(value = "/sync")
    public Wrapper sync() {

        DeviceAlertDto dto = new DeviceAlertDto();

        omcRpcService.syncAlertMsgToShiftLog(dto);

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @PostMapping(value = "/open")
    public Wrapper openDoors(@RequestBody CardOptDoorDto cardOptDoorDto) {

        cardMessageHandler.openDoor(cardOptDoorDto.getDeviceNo(), cardOptDoorDto.getDoors());

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @PostMapping(value = "/close")
    public Wrapper closeDoors(@RequestBody CardOptDoorDto cardOptDoorDto) {

        cardMessageHandler.closeDoor(cardOptDoorDto.getDeviceNo(), cardOptDoorDto.getDoors());

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @PostMapping(value = "/sendWebsocketMessageWithStatus")
    public Wrapper sendWebsocketMessageWithStatus(@RequestBody DeviceStatus deviceStatus) {

        webSocketUtils.pushWebsocketMessageWithStatus(deviceStatus);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @PostMapping(value = "/updateVersion")
    public Wrapper updateVersion(@RequestBody DeviceUpdateDto deviceUpdateDto) {

        // 1.查询版本文件
        DmcCsunVersionFile dmcCsunVersionFile = new DmcCsunVersionFile();
        dmcCsunVersionFile.setFileType(deviceUpdateDto.getFileType());
        dmcCsunVersionFile.setFileVersion(deviceUpdateDto.getFileVersion());
        dmcCsunVersionFile = dmcCsunVersionFileService.selectOne(dmcCsunVersionFile);
        if (PublicUtil.isEmpty(dmcCsunVersionFile)) {
            return WrapMapper.error("版本文件不存在");
        }

        // 2.根据版本和版本文件查询设备编号
        List<DmcDevice> dmcDeviceList = dmcDeviceService.queryUpdateDeviceList(deviceUpdateDto);
        if (PublicUtil.isEmpty(dmcDeviceList)) {
            return WrapMapper.error("暂无设备需要升级");
        }

        String fileUrl = cmnyProperties.getDevice().getFile().getUrl() + deviceUpdateDto.getFileUrl();

        switch (dmcCsunVersionFile.getFileType()) {
            case DeviceConstant.CSUN_CAMERA :
                cameraMessageHandler.sendUpdateVersionMsg(dmcDeviceList, dmcCsunVersionFile, fileUrl);
                break;
            case DeviceConstant.CSUN_GATEWAY :
                gatewayMessageSender.sendUpdateVerMsg(dmcDeviceList, dmcCsunVersionFile, fileUrl);
                break;
            case DeviceConstant.CSUN_SLEEP :
                sleepMessageSender.sendUpdateVerMsg(dmcDeviceList, dmcCsunVersionFile, fileUrl);
                break;
        }

        return WrapMapper.ok();
    }

    @PostMapping(value = "/sendDataMsg")
    public Wrapper sendDataMsg(@RequestBody String deviceNo, String state) {

        sleepMessageSender.sendDataMsg(deviceNo, state);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }


    @ApiOperation(httpMethod = "POST", value = "下载模板")
    @RequestMapping(value = "/downTemplate")
    public void downTemplate(HttpServletResponse response) {





        String filename = cmnyMessageSource.getMessage("excel.down.name", null);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.device.no", null));
        names.add(cmnyMessageSource.getMessage("excel.device.deviceName", null));
        names.add(cmnyMessageSource.getMessage("excel.device.productionDate.template", null));
        names.add(cmnyMessageSource.getMessage("excel.device.batch", null));
        names.add(cmnyMessageSource.getMessage("excel.device.managentNo", null));
        names.add(cmnyMessageSource.getMessage("excel.device.customerNo", null));
        //names.add(cmnyMessageSource.getMessage("excel.device.tilingDate.template", null));
        names.add(cmnyMessageSource.getMessage("excel.device.shipmentDate.template", null));
        names.add(cmnyMessageSource.getMessage("excel.device.initializeDate.template", null));
        names.add(cmnyMessageSource.getMessage("excel.device.deviceVersion", null));
        names.add(cmnyMessageSource.getMessage("excel.device.remark", null));


        ExcelUtil.downExcel(response,filename,names);



    }

    @ApiOperation(httpMethod = "POST", value = "导出设备信息")
    @RequestMapping(value = "/derive")
    public void derive(HttpServletResponse response,String deviceIds) throws IOException {


        List<Long> ids = new ArrayList<>();
        String[] split = deviceIds.split(";");
        for (String s : split) {
            ids.add(Long.parseLong(s));
        }


        DeviceDeriveExcelDto deriveExcelDto = new DeviceDeriveExcelDto();

        deriveExcelDto.setDeviceIds(ids);

        List<PropertyManagementDeviceVo> deviceVoList = dmcDeviceService.selectDeviceByDeviceIds(deriveExcelDto);


        String filename = cmnyMessageSource.getMessage("excel.down.name", null);



        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");
            //  JxlExcelUtils.fcReportToExcel(response, dmcDeviceAlertLifeRoomVos, sheetName);

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);
            Label no = new Label(0, 0, cmnyMessageSource.getMessage("excel.device.no", null));
            Label deviceName = new Label(1, 0, cmnyMessageSource.getMessage("excel.device.deviceName", null));
            Label productionDate=new Label(2,0,cmnyMessageSource.getMessage("excel.device.productionDate", null));
            Label batch=new Label(3,0,cmnyMessageSource.getMessage("excel.device.batch", null));
            Label managentNo=new Label(4,0,cmnyMessageSource.getMessage("excel.device.managentNo", null));
            Label customerNo=new Label(5,0,cmnyMessageSource.getMessage("excel.device.customerNo", null));
            Label shipmentDate=new Label(6,0,cmnyMessageSource.getMessage("excel.device.shipmentDate", null));
            Label initializeDate=new Label(7,0,cmnyMessageSource.getMessage("excel.device.initializeDate", null));
            Label deviceVersion=new Label(8,0,cmnyMessageSource.getMessage("excel.device.deviceVersion", null));
            Label remark=new Label(9,0,cmnyMessageSource.getMessage("excel.device.remark", null));


            //设置列宽
            sheet.setColumnView(0, 25);
            sheet.setColumnView(1, 25);
            sheet.setColumnView(2, 25);
            sheet.setColumnView(3, 25);
            sheet.setColumnView(4, 25);
            sheet.setColumnView(5, 25);
            sheet.setColumnView(6, 25);
            sheet.setColumnView(7, 25);
            sheet.setColumnView(8, 25);
            sheet.setColumnView(9, 25);

            //填入列名
            sheet.addCell(no);
            sheet.addCell(deviceName);
            sheet.addCell(productionDate);
            sheet.addCell(batch);
            sheet.addCell(managentNo);
            sheet.addCell(customerNo);
            sheet.addCell(shipmentDate);
            sheet.addCell(initializeDate);
            sheet.addCell(deviceVersion);
            sheet.addCell(remark);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            //写入数据
            for (int i = 0; i < deviceVoList.size(); i++) {

                sheet.addCell(new Label(0,i+1,deviceVoList.get(i).getDeviceNo()));
                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getDeviceName())){
                    sheet.addCell(new Label(1,i+1,deviceVoList.get(i).getDeviceName()));
                }
                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getProductionDate())){
                    sheet.addCell(new Label(2,i+1,simpleDateFormat.format(deviceVoList.get(i).getProductionDate())));
                }
                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getBatch())){
                    sheet.addCell(new Label(3,i+1,deviceVoList.get(i).getBatch()));
                }
                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getManagentNo())){
                    sheet.addCell(new Label(4,i+1,deviceVoList.get(i).getManagentNo()));
                }
                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getCustomerNo())){
                    sheet.addCell(new Label(5,i+1,deviceVoList.get(i).getCustomerNo()));
                }

                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getShipmentDate())){
                    sheet.addCell(new Label(6,i+1,simpleDateFormat.format(deviceVoList.get(i).getShipmentDate())));
                }
                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getInitializeDate())){
                    sheet.addCell(new Label(7,i+1, simpleDateFormat.format(deviceVoList.get(i).getInitializeDate())));
                }
                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getDeviceVersion())){
                    sheet.addCell(new Label(8,i+1, deviceVoList.get(i).getDeviceVersion()));
                }

                if(PublicUtil.isNotEmpty(deviceVoList.get(i).getRemark())){
                    sheet.addCell(new Label(9,i+1, deviceVoList.get(i).getRemark()));
                }

            }
            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    /**
     * 导出门禁出入记录信息
     */
    @ApiOperation(httpMethod = "GET", value = "导出门禁出入记录信息")
    @RequestMapping(value = "/deriveDmcRfidEvent")
    public void deriveDmcRfidEvent(HttpServletResponse response, RfidEventPageQuery rfidEventPageQuery) throws IOException {

        List<DmcRfidEvent> dmcRfidEventList = dmcRfidEventService.queryDmcRfidEventListWithPage(rfidEventPageQuery);

        String filename = cmnyMessageSource.getMessage("excel.rfid.event.down.name", null);

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");
            //  JxlExcelUtils.fcReportToExcel(response, dmcDeviceAlertLifeRoomVos, sheetName);

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            //初始化表
            ArrayList<String> names = new ArrayList<>();

//            names.add(cmnyMessageSource.getMessage("excel.rfid.event.readerId", null));
            names.add(cmnyMessageSource.getMessage("excel.rfid.event.readerNo", null));
//            names.add(cmnyMessageSource.getMessage("excel.rfid.event.labelId", null));
            names.add(cmnyMessageSource.getMessage("excel.rfid.event.labelNo", null));
//            names.add(cmnyMessageSource.getMessage("excel.rfid.event.oldmanId", null));
            names.add(cmnyMessageSource.getMessage("excel.rfid.event.oldmanName", null));
//            names.add(cmnyMessageSource.getMessage("excel.rfid.event.roleId", null));
//            names.add(cmnyMessageSource.getMessage("excel.rfid.event.roleCode", null));
            names.add(cmnyMessageSource.getMessage("excel.rfid.event.roleName", null));
            names.add(cmnyMessageSource.getMessage("excel.rfid.event.location", null));
//            names.add(cmnyMessageSource.getMessage("excel.rfid.event.groupId", null));

            for (Integer i = 0; i < names.size(); i++) {

                //设置列宽
                sheet.setColumnView(i, 25);
                //写入列名
                sheet.addCell(new Label(i,0,names.get(i)));

            }

//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            //写入数据
            for(int i=0;i<dmcRfidEventList.size();i++){
//                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getReaderId())){
//                    sheet.addCell(new Label(0,i+1,dmcRfidEventList.get(i).getReaderId().toString()));
//                }
                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getReaderNo())){
                    sheet.addCell(new Label(0,i+1,dmcRfidEventList.get(i).getReaderNo()));
                }
//                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getLabelId())){
//                    sheet.addCell(new Label(2,i+1,dmcRfidEventList.get(i).getLabelId().toString()));
//                }
                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getLabelNo())){
                    sheet.addCell(new Label(1,i+1,dmcRfidEventList.get(i).getLabelNo()));
                }
//                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getOldmanId())){
//                    sheet.addCell(new Label(4,i+1,dmcRfidEventList.get(i).getOldmanId().toString()));
//                }
                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getOldmanName())){
                    sheet.addCell(new Label(2,i+1,dmcRfidEventList.get(i).getOldmanName()));
                }
//                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getRoleId())){
//                    sheet.addCell(new Label(6,i+1,dmcRfidEventList.get(i).getRoleId().toString()));
//                }
//                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getRoleCode())){
//                    sheet.addCell(new Label(7,i+1,dmcRfidEventList.get(i).getRoleCode()));
//                }
                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getRoleName())){
                    sheet.addCell(new Label(3,i+1,dmcRfidEventList.get(i).getRoleName()));
                }
                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getLocation())){
                    sheet.addCell(new Label(4,i+1,dmcRfidEventList.get(i).getLocation()));
                }
//                if(PublicUtil.isNotEmpty(dmcRfidEventList.get(i).getGroupId())){
//                    sheet.addCell(new Label(10,i+1,dmcRfidEventList.get(i).getGroupId().toString()));
//                }
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 巡更记录报表导出（巡更人，位置，巡更棒编号，巡更点编号，巡更时间）
     *
     * @param response
     *
     * @param patrolRecordQueryDto
     */
    @RequestMapping("/patrolRecordDown")
    public void patrolRecordDown(HttpServletResponse response, PatrolRecordQueryDto patrolRecordQueryDto) {

        patrolRecordQueryDto.setPageSize(65535);
        List<PatrolRecordVo> patrolRecordVoList = dmcPatrolRecordService
                .queryListWithPage(patrolRecordQueryDto);

        String filename = "巡更记录表";
        String titleName = "巡更记录表";
        String sheetName = "巡更记录表";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

             JxlExcelUtils.patrolRecordDown(response, patrolRecordVoList, titleName, sheetName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 巡更提醒报表导出（位置，巡更棒编号，巡更点编号，巡更时间）
     *
     * @param response
     *
     * @param patrolReminderQueryDto
     */
    @RequestMapping("/patrolReminderDown")
    public void patrolReminderDown(HttpServletResponse response, PatrolReminderQueryDto patrolReminderQueryDto) {

        patrolReminderQueryDto.setPageSize(65535);
        List<PatrolReminderVo> patrolReminderVoList = dmcPatrolReminderService
                .queryListWithPage(patrolReminderQueryDto);

        String filename = "巡更提醒表";
        String titleName = "巡更提醒表";
        String sheetName = "巡更提醒表";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

             JxlExcelUtils.patrolReminderDown(response, patrolReminderVoList, titleName, sheetName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
